import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter01/pages/getx/count_controller.dart';
class OtherPage extends StatefulWidget {
  const OtherPage({Key? key}) : super(key: key);

  @override
  _OtherPageState createState() => _OtherPageState();
}

class _OtherPageState extends State<OtherPage> {
  final count1 = 10.0.obs;

  final num1 = 1.obs;
  final num2 = 1.obs;

  int get sum{
    return num1.value + num2.value;
  }

  final list = [0,1].obs;

  final map = {"num":1}.obs;

  final user = User(name: 'John', last: 'Doe', age: 33).obs;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    print("other page build");
    Future.value(0).then((num){
      Get.snackbar("路由传参：", "${Get.arguments}",);
    });
    // 你可以让Get找到一个正在被其他页面使用的Controller，并将它返回给你。
    final c = Get.find<CountController>();
    return Scaffold(
      appBar: AppBar(title:Text("Other")),
      body:SingleChildScrollView(
        child: SafeArea(
          child: Center(
            child:Column(
              children:[
                Container(
                  child:Text('路由传参：${Get.arguments}')
                ),
                Divider(),
                Obx(()=>Text('count:${c.count}')),
                GetX<CountController>(builder: (controller){
                  return GestureDetector(onTap:(){
                    controller.increment();
                  },child: Text('count:${controller.count}'));
                },),
                ElevatedButton(onPressed: c.increment, child: Text("count++")),
                ElevatedButton(onPressed: CountController.to.increment, child: Text("count++")),
                Divider(),
                Obx(()=>Text('${count1.value}')),
                ElevatedButton(onPressed: (){
                  count1.value /= 2;
                }, child: Text('decrement')),
                Divider(),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children:[
                    Obx(()=>GestureDetector(onTap:(){
                      num1.value++;
                    },child: Text(num1.value.toString()))),
                    Text('+'),
                    Obx(()=>GestureDetector(onTap:(){
                      num2.value++;
                    },child: Text(num2.value.toString()))),
                    Text('='),
                    Obx(()=>Text(sum.toString()))
                  ]
                ),
                Divider(),
                Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children:[
                    Obx(()=>Text(list.toString())),
                    ElevatedButton(onPressed: (){
                      list.value = [1,2,3];
                    }, child: Text("arr reset"),),
                    ElevatedButton(onPressed: (){
                       list.add(Random().nextInt(10));
                    }, child: Text("arr add")),
                    ElevatedButton(onPressed: (){
                      list.shuffle();
                    }, child: Text("arr shuffle")),
                    ElevatedButton(onPressed: (){
                      list.removeLast();
                    }, child: Text("arr remove"))
                  ]
                ),
                Divider(),
                Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Obx(()=>Text(map.toString())),
                    ElevatedButton(onPressed: (){
                      map["num"] = Random().nextInt(5);
                    }, child: Text("map change")),
                    ElevatedButton(onPressed: (){
                      map["num1"] = Random().nextInt(5);
                    }, child: Text("map add")),
                    ElevatedButton(onPressed: (){
                      map.remove('num1');
                    }, child: Text("map delete")),
                  ],
                ),
                Divider(),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Obx(()=>Text('${user}')),
                    ElevatedButton(onPressed: (){
                      user.update((val) {
                        val?.name = 'Roi';
                      });
                    }, child: Text("User change"))
                  ],
                )
              ]
            )
          ),
        ),
      )
    );
  }
}

class User {
  String name, last;
  int age;
  User({required this.name, required this.last, required this.age});

  @override
  String toString() => '$name $last, $age years old';
}

